<template>
  <el-dialog title="分配角色" :visible.sync="isShow" @close="closeEvent">
    <el-checkbox
      v-for="item in list"
      :key="item.id"
      v-model="roleIds"
      :label="item.id"
    >{{ item.name }}</el-checkbox>
    <template>
      <div style="text-align: center; margin-top: 20px">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="isShow = false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysRole } from '@/api/setting'
import { sysUser } from '@/api/user'
import { sysUserAssignRoles } from '@/api/employees'
export default {
  data() {
    return {
      isShow: false,
      list: [],
      roleIds: [],
      id: ''
    }
  },
  created() {
    // this.getData()
  },
  methods: {
    async getData() {
      if (this.list.length === 0) {
        const res = await sysRole({ page: 1, pagesize: 9999 })
        this.list = res.data.rows
        console.log('角色列表', res)
      }
    },
    // 获取当前用户的角色数据
    async getUserRoleIds(id) {
      this.id = id
      const res = await sysUser(id)
      this.roleIds = res.data.roleIds
      console.log('获取当前用户的角色数据', res)
    },
    // 确定点击事件
    async submit() {
      // 调用设置用户角色api
      await sysUserAssignRoles({
        id: this.id,
        roleIds: this.roleIds
      })
      // 提示
      this.$message.success('修改角色成功')
      // 关闭弹框
      this.isShow = false
    },
    // 关闭弹框时清空已选择
    closeEvent() {
      this.roleIds = []
    }
  }
}
</script>
<style></style>
